package com.wxcms.domain;

import java.io.File;
import java.io.Serializable;

import org.apache.log4j.Logger;

import me.chanjar.weixin.common.bean.WxAccessToken;

public class WxCpMyConfigStorage  implements me.chanjar.weixin.cp.api.WxCpConfigStorage ,Serializable {
  	  Logger log = Logger.getLogger(this.getClass());
	  private volatile String corpid;
	  private volatile String corpSecret;

	  private volatile String token;
	  private volatile String accessToken;
	  private volatile String aesKey;
	  private volatile String agentId;
	  private volatile long expiresTime;

	  private volatile String oauth2redirectUri;

	  private volatile String http_proxy_host;
	  private volatile int http_proxy_port;
	  private volatile String http_proxy_username;
	  private volatile String http_proxy_password;

	  private volatile String jsapiTicket;
	  private volatile String ticket;
	  
	  private volatile long ticketExpiresTime;
	  
	  private volatile long jsapiTicketExpiresTime;

	  protected volatile File tmpDirFile;
	  
	  private volatile String province;
	  private volatile String  provincename;

	  private volatile String url;
	  
	  
	  private volatile  String name;
	  
	  
	  private volatile  String icon;
	  private volatile  String corpicon;
	  private String groupid;
	  
	  public String getAccessToken() {
	    return this.accessToken;
	  }

	  public boolean isAccessTokenExpired() {
	    return System.currentTimeMillis() > this.expiresTime;
	  }

	  public void expireAccessToken() {
	    this.expiresTime = 0;
	  }

	  public synchronized void updateAccessToken(WxAccessToken accessToken) {
	    updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
	  }
	  
	  public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
	    this.accessToken = accessToken;
	    this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
	    log.warn(String.format("======================更新token  %s\n", accessToken));
	  }

	  
	  public String getJsapiTicket() {
	    return jsapiTicket;
	  }

	  public void setJsapiTicket(String jsapiTicket) {
	    this.jsapiTicket = jsapiTicket;
	  }

	  public long getJsapiTicketExpiresTime() {
	    return jsapiTicketExpiresTime;
	  }

	  public void setJsapiTicketExpiresTime(long jsapiTicketExpiresTime) {
	    this.jsapiTicketExpiresTime = jsapiTicketExpiresTime;
	  }

	  public boolean isJsapiTicketExpired() {
	    return System.currentTimeMillis() > this.jsapiTicketExpiresTime;
	  }
	  public boolean isTicketExpired() {
		    return System.currentTimeMillis() > this.ticketExpiresTime;
      }
	  public synchronized void updateTicket(String ticket, String group_id,int expiresInSeconds) {
		    this.ticket = ticket;
		    this.setGroupid(group_id);
		    // 预留200秒的时间
		    this.ticketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
	 }
	  public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
	    this.jsapiTicket = jsapiTicket;
	    // 预留200秒的时间
	    this.jsapiTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
	  }

	  public void expireJsapiTicket() {
	    this.jsapiTicketExpiresTime = 0;
	  }
	  public void expireTicket() {
		    this.ticketExpiresTime = 0;
	   }
	 

	  public String getCorpSecret() {
	    return this.corpSecret;
	  }

	  public String getToken() {
	    return this.token;
	  }

	  public long getExpiresTime() {
	    return this.expiresTime;
	  }

	 

	  public void setCorpSecret(String corpSecret) {
	    this.corpSecret = corpSecret;
	  }

	  public void setToken(String token) {
	    this.token = token;
	  }

	  public String getAesKey() {
	    return aesKey;
	  }

	  public void setAesKey(String aesKey) {
	    this.aesKey = aesKey;
	  }

	  public void setAccessToken(String accessToken) {
	    this.accessToken = accessToken;
	  }

	  public void setExpiresTime(long expiresTime) {
	    this.expiresTime = expiresTime;
	  }

	  public String getAgentId() {
	    return agentId;
	  }

	  public void setAgentId(String agentId) {
	    this.agentId = agentId;
	  }

	 
	  public String getOauth2redirectUri() {
	    return this.oauth2redirectUri;
	  }

	  public void setOauth2redirectUri(String oauth2redirectUri) {
	    this.oauth2redirectUri = oauth2redirectUri;
	  }

	  public String getHttp_proxy_host() {
	    return http_proxy_host;
	  }

	  public void setHttp_proxy_host(String http_proxy_host) {
	    this.http_proxy_host = http_proxy_host;
	  }

	  public int getHttp_proxy_port() {
	    return http_proxy_port;
	  }

	  public void setHttp_proxy_port(int http_proxy_port) {
	    this.http_proxy_port = http_proxy_port;
	  }

	  public String getHttp_proxy_username() {
	    return http_proxy_username;
	  }

	  public void setHttp_proxy_username(String http_proxy_username) {
	    this.http_proxy_username = http_proxy_username;
	  }

	  public String getHttp_proxy_password() {
	    return http_proxy_password;
	  }

	  public void setHttp_proxy_password(String http_proxy_password) {
	    this.http_proxy_password = http_proxy_password;
	  }

	  @Override
	  public String toString() {
	    return "WxCpInMemoryConfigStorage{" +
	        "corpId='" + corpid + '\'' +
	        ", corpSecret='" + corpSecret + '\'' +
	        ", token='" + token + '\'' +
	        ", accessToken='" + accessToken + '\'' +
	        ", aesKey='" + aesKey + '\'' +
	        ", agentId='" + agentId + '\'' +
	        ", expiresTime=" + expiresTime +
	        ", http_proxy_host='" + http_proxy_host + '\'' +
	        ", http_proxy_port=" + http_proxy_port +
	        ", http_proxy_username='" + http_proxy_username + '\'' +
	        ", http_proxy_password='" + http_proxy_password + '\'' +
	        ", jsapiTicket='" + jsapiTicket + '\'' +
	        ", jsapiTicketExpiresTime='" + jsapiTicketExpiresTime + '\'' +
	        ", tmpDirFile='" + tmpDirFile + '\'' +
	        '}';
	  }

	  public File getTmpDirFile() {
	    return tmpDirFile;
	  }

	  public void setTmpDirFile(File tmpDirFile) {
	    this.tmpDirFile = tmpDirFile;
	  }

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	@Override
	public String getCorpId() {
		// TODO Auto-generated method stub
		return corpid;
	}

	public String getCorpicon() {
		return corpicon;
	}

	public void setCorpicon(String corpicon) {
		this.corpicon = corpicon;
	}

	public String getProvincename() {
		return provincename;
	}

	public void setProvincename(String provincename) {
		this.provincename = provincename;
	}

	public String getTicket() {
		return ticket;
	}

	public void setTicket(String ticket) {
		this.ticket = ticket;
	}

	public long getTicketExpiresTime() {
		return ticketExpiresTime;
	}

	public void setTicketExpiresTime(long ticketExpiresTime) {
		this.ticketExpiresTime = ticketExpiresTime;
	}

	public String getGroupid() {
		return groupid;
	}

	public void setGroupid(String groupid) {
		this.groupid = groupid;
	}
}
